package Passageiros;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class DataSourceDAO 
{
	protected Connection connection;
	private static final String MySQLDriver 	= "com.mysql.jdbc.Driver";  
	private static final String URL 			= "jdbc:mysql://localhost/ads4" ;
	private static final String USER			= "root" ;
	private static final String PASSWORD 		= "synap2008" ;
	
	private static final String OracleDriver 	= "oracle.jdbc.driver.OracleDriver";  
	private static final String OracleURL 		= "jdbc:oracle:thin:@localhost:1521:XE" ;
	private static final String OracleUSER		= "system" ;
	private static final String OraclePASSWORD 	= "123456" ;
	
	public DataSourceDAO( ) 
	{
		init( ) ;
	}

	private void init( ) 
	{
		try 
		{
			Class.forName( MySQLDriver ) ;
			connection = DriverManager.getConnection( URL, USER,PASSWORD ) ;
		} 
		catch ( Exception e ) 
		{
			e.printStackTrace( ) ;
			throw new RuntimeException(" Impossivel localizar DataSource " ) ;
		}       
	}
	
	public final void connect( ) throws SQLException 
	{
		if ( connection == null ) 
		{
			init( );
		}
	}

	public final void close( ) throws SQLException 
	{
		if ( connection != null ) 
		{
			connection.close( );
		}
	}

	public final void commit( ) 
	{
		try 
		{
			if ( connection != null ) 
			{
				connection.commit( ) ;
			}
		} 
		catch ( SQLException e ) 
		{
			e.printStackTrace();
			throw new RuntimeException( "Impossivel comitar!", e ) ;
		}
	}

	public final void rollback( )  
	{
		try 
		{
			if ( connection != null ) 
			{
				connection.rollback( ) ;
			}
		} 
		catch ( SQLException e ) 
		{
			e.printStackTrace();
			throw new RuntimeException( "Impossivel desfazer!", e ) ;
		}
	}
}
